package org.as4game.common.math
{
	import flash.geom.Transform;
	import flash.geom.Vector3D;

	/**
	 * 描述了一个变化
	 */
	public class Translate
	{
		public var orientation:Quaternion;				// 旋转
		public var position:Vector3;				// 位置
		public var scale:Vector3;					// 缩放
		
		
		// 构造函数, 初始为默认值
		public function Translate(){
			orientation = new Quaternion;
			position = new Vector3;
			scale = new Vector3(1, 1, 1);
		}
		
		/**
		 * 组合一个变化, dst = this + trans, 不修改自身. 这段代码非常复杂, 因此尽量优化减少调用次数
		 * 
		 * @param trans 新增加的变化
		 * @param dst 组合保存的目标
		 * 
		 * 此段代码来自 Ogre, Node::updateFromParentImpl 
		 */
		public function appendTranslate( local:Translate, dst:Translate=null ):Translate{
			if( !dst ) dst = new Translate;
			var parent:Translate = this;
			
			var orientation:Quaternion = new Quaternion;
			var scale:Vector3 = new Vector3;
			var position:Vector3 = new Vector3;
			
			Math2.multiply_q_q( orientation, parent.orientation, local.orientation);		// 16
			Math2.multiply_v3_v3( scale, parent.scale, local.scale);						// 3
			Math2.multiply_v3_v3( position, parent.scale, local.position);					// 3
			Math2.multiply_q_v3( position, parent.orientation, position);					// 18
			Math2.add_v3_v3(position, position, parent.position );
			
			dst.orientation = orientation;
			dst.position = position;
			dst.scale = scale;
			return dst;
		}
		
		public function clone():Translate{
			var dst:Translate = new Translate;
			cloneTo( dst );
			return dst;
		}
		
		// clone
		public function cloneTo( dst:Translate ):void{
			var src:Translate = this;
			src.orientation.cloneTo( dst.orientation );
			src.position.cloneTo( dst.position );
			src.scale.cloneTo( dst.scale );
		}
	}
}